package com.fhz.day002;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.fhz.day002.databinding.RvLayoutBinding;

import java.util.List;

/**
 * 时间:2022/3/21
 *
 * @author msfeng
 * 简述: TODO
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    List<Food> list;

    public MyAdapter(List<Food> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_layout, parent, false);
        RvLayoutBinding bind = DataBindingUtil.bind(inflate);
        return new ViewHolder(bind);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
        holder.binding.setFood(list.get(position));
        holder.binding.executePendingBindings();
    }


    @BindingAdapter({"imageUrl"})
    public static void setImageUrl(ImageView imageView,String url){
        Glide.with(imageView.getContext()).load(url).into(imageView);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        RvLayoutBinding binding;

        public ViewHolder(@NonNull RvLayoutBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }
}

